package com.weipai.education.service.impl;

import com.weipai.education.dao.InventoryDao;
import com.weipai.education.dao.PurchaseDao;
import com.weipai.education.dao.UserDao;
import com.weipai.education.dao.UserEarningsDao;
import com.weipai.education.enums.RetCode;
import com.weipai.education.enums.UserEarningsType;
import com.weipai.education.enums.UserType;
import com.weipai.education.model.User;
import com.weipai.education.model.api.PageResult;
import com.weipai.education.model.api.Response;
import com.weipai.education.service.UserEarningsService;
import com.weipai.education.utils.SessionUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/12/18 18:56
 */
@Service
public class UserEarningsServiceImpl implements UserEarningsService {

    @Autowired
    private UserEarningsDao userEarningsDao;
    @Autowired
    private PurchaseDao purchaseDao;
    @Autowired
    private SessionUserUtil sessionUserUtil;
    @Autowired
    private UserDao userDao;
    @Autowired
    private InventoryDao inventoryDao;

    @Override
    public Response list(Integer page, Integer size, String type, String accountNumber, String startTime, String endTime) {
        Response response = new Response();

        String userId = sessionUserUtil.getUser().getId().toString();
        User user = userDao.selectByPrimaryKey(userId); //当前用户的信息
        if (user.getType() == UserType.HQ_USER.getType()) { //如归是平台的账号
            userId = "-1";
        }
        Integer start = (page - 1) * size;
        List<Map<String, Object>> earningsList = userEarningsDao.list(start, size, type, accountNumber, startTime, endTime, userId);
        Integer totalNum = userEarningsDao.listAmount(type, accountNumber, startTime, endTime, userId);

        //总收益
        BigDecimal allRewardPrice = userEarningsDao.getAllRewardPrice(null, userId,null);

        //本月收益
        BigDecimal monthRewardPrice = userEarningsDao.getAllRewardPrice("month", userId,null);

        BigDecimal monthReturnPrice = userEarningsDao.getAllRewardPrice("month",userId,UserEarningsType.SALE_RETURN.getType());
        //拿货奖
        List<Integer> goodsReward = new ArrayList<>();
        goodsReward.add(UserEarningsType.GET_GOODS_REWARD.getType());
        goodsReward.add(UserEarningsType.GET_ADDRESS_REWARD.getType());

        //拿货收益
        BigDecimal getGoodsRewardPrice = userEarningsDao.getDifferentRewardPrice(goodsReward, userId);

        //退货钱
        List<Integer> returnGoodsReward = new ArrayList<>();
        returnGoodsReward.add(UserEarningsType.SALE_RETURN.getType());
        BigDecimal returnPrice = userEarningsDao.getDifferentRewardPrice(goodsReward, userId);
        if(returnPrice != null){
            allRewardPrice = allRewardPrice.subtract(returnPrice);
            getGoodsRewardPrice = getGoodsRewardPrice.subtract(returnPrice);
        }
        if(monthReturnPrice != null ){
            monthRewardPrice = monthRewardPrice.subtract(monthReturnPrice);
        }

        //推荐奖
        List<Integer> recomed = new ArrayList<>();
        recomed.add(UserEarningsType.RECOMMEND_REWARD.getType());
        recomed.add(UserEarningsType.ADDRESS_RECOMMEND_REWARD.getType());
        recomed.add(UserEarningsType.ADDRESS_REWARD.getType());
        //推荐收益
        BigDecimal recommendRewardPrice = userEarningsDao.getDifferentRewardPrice(recomed, userId);

        PageResult<Map<String, Object>> pageList = new PageResult<>(earningsList, page, size, totalNum);
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        response.put("allRewardPrice", allRewardPrice);
        response.put("monthRewardPrice", monthRewardPrice);
        response.put("getGoodsRewardPrice", getGoodsRewardPrice);
        response.put("recommendRewardPrice", recommendRewardPrice);
        return response;
    }

    @Override
    public Response pcList(Integer page, Integer size, String type, String userId) {
        Response response = new Response();
        Integer start = (page - 1) * size;
        Integer userId2 = sessionUserUtil.getUser().getId();
        User user = userDao.selectByPrimaryKey(userId2.toString());
        Map<String, Object> map = null;
        if (user.getType() == UserType.HQ_USER.getType()) {
            userId = "-1";
        }

        List<Map<String, Object>> purchaseList = null;
        Integer totalNum = null;
        if (type.equals("days")) {
            //查询今天的订单
            purchaseList = purchaseDao.getDayPurchase(userId);
            totalNum = purchaseDao.getDayPurchaseAmount(userId);
        } else if (type.equals("weeks")) {
            //按周来查询订单
            map = purchaseDao.getAllAmount(userId);
            purchaseList = purchaseDao.getWeekPurchase(start, size, userId);
            totalNum = purchaseDao.getWeekPurchaseAmount(userId);
        } else if (type.equals("months")) {
            //按月来查询订单
            map = purchaseDao.getAllAmount(userId);
            purchaseList = purchaseDao.getMonthPurchase(start, size, userId);
            totalNum = purchaseDao.getMonthPurchaseAmount(userId);
        }
        PageResult<Map<String, Object>> pageList = new PageResult<>(purchaseList, page, size, totalNum);
        response.put("all", map);
        response.put("pageList", pageList);
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    @Override
    public Response homePage(String userId) {
        Response response = new Response();

        Integer userId2 = sessionUserUtil.getUser().getId();
        User user = userDao.selectByPrimaryKey(userId2.toString());
        if (user.getType() == UserType.HQ_USER.getType()) {
            userId = "-1";
        }

        //查询加盟商总数
        int userAmount = userDao.listAmount(null, null, userId);

        //产品的总销量
        int productNum = inventoryDao.getSaleNumber(userId);

        //近七天的销售量
        List<Map<String, Object>> list = inventoryDao.getSaleNumberWeek(userId);

        response.put("userAmount", userAmount);
        response.put("productNum", productNum);
        response.put("week", list);
        response.setCode(RetCode.SUCCESS);
        return response;

    }

}
